//
//  StapleViewController.m
//  美食百科
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "StapleViewController.h"
#import "StapleCollectionViewCell.h"
#import "NewWorking.h"
#import "ChooseView.h"
#import "ClassModel.h"
#import "MJRefresh.h"
#import "InforViewController.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface StapleViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,ChooseViewDelegate>
@property(nonatomic,retain)UICollectionView *collection;
@property(nonatomic,retain)UIView *lineView;
@property(nonatomic,assign)CGFloat buttonWidth;
@property(nonatomic,assign)CGFloat originY;
@property(nonatomic,assign)NSInteger order;
@property(nonatomic,retain)ChooseView *choose;
@property(nonatomic,assign)BOOL chooseBool;
//@property(nonatomic,assign)NSInteger chooseNumber;
@property(nonatomic,assign)NSInteger IdNumber;
@property(nonatomic,copy)NSString *nameString;
@property(nonatomic,assign)NSInteger number;

//@property(nonatomic,assign)BOOL orderBool;

//@property(nonatomic,retain)NSMutableArray *stapleArray;
@end

@implementation StapleViewController
- (void)dealloc
{ [self.collection removeObserver:self forKeyPath:@"contentOffset" context:@"观察self.collection偏移量"];
    
    [_classModel release];
    [_kindStr release];
    [_array release];
    
    [_collection release];
    [_lineView release];
    [_choose release];
    [_nameString release];
 //   [_stapleArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    if (self.array.count != 0) {
        UIBarButtonItem *rightItemFirst = [[UIBarButtonItem alloc]initWithTitle:@"全部" style:UIBarButtonItemStyleDone target:self action:nil];
    UIBarButtonItem *rightItemSecond = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-caidanshu"] style:UIBarButtonItemStylePlain target:self action:@selector(chooseAction:)];
    
    self.navigationItem.rightBarButtonItems = @[rightItemSecond,rightItemFirst];
    }
    UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
    UIBarButtonItem *leftItemSecond = [[UIBarButtonItem alloc]initWithTitle:self.classModel.name style:UIBarButtonItemStyleDone target:self action:nil];
    
    self.navigationItem.leftBarButtonItems = @[leftItemFirst,leftItemSecond];
    
    
       
    
    self.chooseBool = YES;
    //self.chooseNumber = 1;
    self.number = 1;
    
    
    NSArray *buttonArray = @[@"常见",@"热量↑",@"蛋白质↑",@"脂肪↑"];
    NSArray *buttonColor = @[[UIColor redColor],[UIColor grayColor],[UIColor grayColor],[UIColor grayColor]];
    for (int i= 0; i<4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(kWidth/5.5*i, 0, kWidth/5.5-5, 40);
       self.buttonWidth = button.frame.size.width;
        self.originY = button.bounds.size.height+button.bounds.origin.y;
       // NSLog(@"%f,%f",kWidth/5,kWidth/5-5);
        button.backgroundColor = [UIColor whiteColor];
//        button.layer.cornerRadius = 9;
//        button.layer.masksToBounds =YES;
        [button setTitleColor:buttonColor[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setTitle:buttonArray[i] forState:UIControlStateNormal];
        button.tag = 100+i;
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    //设置列表数据顺序
    UIButton *buttonOrder = [UIButton buttonWithType:UIButtonTypeCustom];
    
    buttonOrder.frame =CGRectMake(kWidth/5.5*4, 0, kWidth/5.5, 40);
    buttonOrder.backgroundColor = [UIColor colorWithRed:0.996 green:0.965 blue:0.251 alpha:1.00];
    [buttonOrder setTitle:@"倒序↓" forState:UIControlStateNormal];
    [buttonOrder setTitleColor:[UIColor colorWithRed:0.000 green:0.914 blue:0.231 alpha:1.00] forState:UIControlStateNormal];
    [buttonOrder addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
//    buttonOrder.layer.cornerRadius = 12;
//    buttonOrder.layer.masksToBounds =YES;
    [self.view addSubview:buttonOrder];
    
    //标线
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.lineView];
    [_lineView release];
   
   // NSLog(@"🐌%ld",self.array.count);
    
    self.order = 1;
    
    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(kWidth, kHeight-(64+self.originY+2));
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing= 0;
//    layout.minimumInteritemSpacing = 0;
//    layout.headerReferenceSize = CGSizeMake(0, 0);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.originY+2, kWidth, kHeight-(64+self.originY+2)) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor clearColor];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.bounces = NO;
    self.collection.pagingEnabled = YES;
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"观察self.collection偏移量"];
    [self.view addSubview:self.collection];
    [_collection release];
    
    [self.collection registerClass:[StapleCollectionViewCell class] forCellWithReuseIdentifier:@"stapleCell"];



}





//按钮与偏移量的联系
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
   // NSLog(@"%@",change);
    NSInteger X = [change[@"new"] CGPointValue].x/kWidth;
    self.lineView.frame = CGRectMake( X *(self.buttonWidth+5), self.originY, self.buttonWidth, 2);
  //  NSLog(@"%ld",X);
    for (int i =0; i<4; i++) {
        
        UIButton *btn= (UIButton *)[self.view viewWithTag:i+100];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
      //  NSLog(@"🍎");
    }
    
    
    
    UIButton *bune =(UIButton *)[self.view viewWithTag:X+100];
    [bune setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    
    
}




-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}




-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    StapleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"stapleCell"forIndexPath:indexPath];
    //cell.rightArray = self.array;
   // cell.pageStart = indexPath.row;
    cell.urlNumber = self.IdNumber;
    cell.kind = self.kindStr;
    cell.valueNumber = [self.classModel.Id integerValue];
    //cell.itemArray = self.stapleArray;
    //cell.orderNumber = self.chooseNumber;
    cell.order_asc = self.order;
    cell.page = self.number;
    cell.row = indexPath.row;
    
    
    //跳转详情页
    cell.block = ^(ClassModel *class){
        InforViewController *information = [[InforViewController alloc]init];
        information.number = 0;
        information.classModel = class;
        
        
        [self.navigationController pushViewController:information animated:YES];
        
        [information release];
    };
    
    return cell;
}









-(void)backAction:(UIBarButtonItem *)item{
    
    [self.navigationController popViewControllerAnimated:YES];
}

//右上角分栏
-(void)chooseAction:(UIBarButtonItem *)item{
    
    if (self.chooseBool&&self.array.count !=0) {
        [UIView animateWithDuration:0.3 animations:^{
            self.choose = [[ChooseView alloc]initWithFrame:CGRectMake(kWidth - kWidth/3, -kHeight/3.5/4*(self.array.count), kWidth/3-5, kHeight/3.5/4*(self.array.count))];
            self.choose.count = self.array.count;
         //   self.choose.backgroundColor = [UIColor blueColor];
            self.choose.nameArray = self.array;
            self.choose.layer.cornerRadius = 10;
            self.choose.layer.masksToBounds = YES;
            NSLog(@"🌲%ld",self.array.count);
            self.choose.delegate = self;
       // self.choose.backgroundColor = [UIColor grayColor];
       [self.view addSubview:self.choose];
        [self.view bringSubviewToFront:self.choose];

        [_choose release];
        
       self.choose.frame = CGRectMake((kWidth - kWidth/3), 0, (kWidth/3-5), kHeight/3.5/4*(self.array.count));
         //  NSLog(@"🍍");
               // self.choose.backgroundColor = [UIColor blueColor];
    }];

        self.chooseBool = !self.chooseBool;
    }else{
        
        [UIView animateWithDuration:0.3 animations:^{
            self.choose.frame = CGRectMake((kWidth - kWidth/3), -kHeight/3.5/4*(self.array.count), (kWidth/3-5), kHeight/3.5/4*(self.array.count));
            
        } completion:^(BOOL finished) {
            
            [self.choose removeFromSuperview];
            NSLog(@"ne");
        }];
        
        self.chooseBool = !self.chooseBool;
    }
    
    
    

//    NSLog(@"🍎");
    //    
//    NSLog(@"🍊");

    
}

//点击分栏改变数据,右上角标题
-(void)getLine:(NSInteger)Id rightName:(NSString *)name
{
    self.IdNumber = Id;
    if (self.nameString != name) {
        [self.collection reloadData];
        self.nameString = name;
    }
    
    
    UIBarButtonItem *rightItemFirst = [[UIBarButtonItem alloc]initWithTitle:name style:UIBarButtonItemStyleDone target:self action:nil];
    UIBarButtonItem *rightItemSecond = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-caidanshu"] style:UIBarButtonItemStylePlain target:self action:@selector(chooseAction:)];
    
    self.navigationItem.rightBarButtonItems = @[rightItemSecond,rightItemFirst];
    
    self.chooseBool = !self.chooseBool;
    
}

-(void)orderAction:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    if (btn.isSelected) {
        [btn setTitle:@"正序↑" forState:UIControlStateNormal];
        NSArray *clickArrayFirst = @[@"常见",@"热量↓",@"蛋白质↓",@"脂肪↓"];
        for (int i =0; i<4; i++) {
            
            UIButton *btn= (UIButton *)[self.view viewWithTag:i+100];
            [btn setTitle:clickArrayFirst[i] forState:UIControlStateNormal];
            //  NSLog(@"🍎");
        }
        
        
        self.order = !self.order;
        
        
        [self.collection reloadData];
        
        
    
    }else{
        [btn setTitle:@"倒序↓" forState:UIControlStateNormal];
        NSArray *clickArraySecond = @[@"常见",@"热量↑",@"蛋白质↑",@"脂肪↑"];
        for (int i =0; i<4; i++) {
            
            UIButton *btn= (UIButton *)[self.view viewWithTag:i+100];
            [btn setTitle:clickArraySecond[i] forState:UIControlStateNormal];
            //  NSLog(@"🍎");
        }

        self.order = !self.order;
        [self.collection reloadData];

    }
}

//按钮与偏移量的联系
-(void)clickAction:(UIButton *)button

{
    
    self.collection.contentOffset = CGPointMake((button.tag -100)*kWidth, 0);
    self.lineView.frame = CGRectMake((button.tag -100)*(self.buttonWidth+5), self.originY, self.buttonWidth, 2);
    
    //[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    for (int i =100; i<104; i++) {
        
        UIButton *btn= (UIButton *)[self.view viewWithTag:i];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
          }
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
        
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
